package edu.whu.rose.clock.kps.algorithm.blinks.search;

public class PotentialTree implements Comparable<PotentialTree> {

	private int root;
	
	private Cursor[] cursors;
	
	private double[] d1;
	private double d2;
	
	private int[] matcheds;
	private int[] predecessors;
	private double[] distances;
	
	public PotentialTree(int root, int l) {
		this.root = root;
		matcheds = new int[l];
		predecessors = new int[l];
		distances = new double[l];
		d1 = new double[l];
		cursors = new Cursor[l];
		for (int i = 0; i < l; i++) {
			distances[i] = Double.MAX_VALUE;
			d1[i] = Double.MAX_VALUE;
		}
	}
	
	public void setMatched(int i, int matched) {
		matcheds[i] = matched;
	}
	
	public void setPredecessor(int i, int predecessor) {
		predecessors[i] = predecessor;
	}
	
	public void setDistance(int i, double distance) {
		distances[i] = distance;
	}
	
	public double getDistance(int i) {
		return distances[i];
	}
	
	public int size() {
		return distances.length;
	}
	
	public double sumDistances() {
		double sum = 0.0d;
		for (int i = 0; i < distances.length; i++) {
			if (distances[i] < Double.MAX_VALUE) {
				sum += distances[i];
			}
			else {
				return Double.MAX_VALUE;
			}
		}
		return sum;
	}
	
//	public double sumLBDistances() {
//		double sum = 0.0d;
//		for (int i = 0; i < distances.length; i++) {
//			if (distances[i] < Double.MAX_VALUE) {
//				sum += distances[i];
//			}
//			else {
//				
//				return Double.MAX_VALUE;
//			}
//		}
//		return sum;
//	}
	
	public boolean isAnswer() {
		for (int i = 0; i < distances.length; i++) {
			if (distances[i] == Double.MAX_VALUE) {
				return false;
			}
		}
		return true;
	}

	public int compareTo(PotentialTree x) {
		if (sumDistances() > x.sumDistances()) {
			return 1;
		}
		else if (sumDistances() < x.sumDistances()) {
			return -1;
		}
		else {
			return 0;
		}
	}

	public int getRoot() {
		return root;
	}

	public int[] getMatcheds() {
		return matcheds;
	}

	public boolean equals(Object x) {
		if (root == ((PotentialTree) x).getRoot()) {
			return true;
		}
		else {
			return false;
		}
	}

	public double getD1(int i) {
		return d1[i];
	}

	public void setD1(int i, double d1) {
		this.d1[i] = d1;
	}

	public double getD2() {
		return d2;
	}

	public void setD2(double d2) {
		this.d2 = d2;
	}
	
	public Cursor getCursor(int i) {
		return cursors[i];
	}

	public void setCursor(int i, Cursor cursor) {
		this.cursors[i] = cursor;
	}

}
